﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Collections;
using Editor.Utilities;

namespace Editor.Elements
{
    public abstract class Element : IComparable, IDictSubject<string>
    {
        public abstract IList ChildElements { get; set; }

        [XmlIgnore]
        public string DefaultName
        {
            get
            {
                return Dictionary[EditorForm.DefaultLanguage];
            }
            set
            {
                Dictionary[EditorForm.DefaultLanguage] = value;
            }
        }

        [XmlIgnore]
        public string CurrentName
        {
            get
            {
                return Dictionary[EditorForm.CurrentLanguage];
            }
            set
            {
                Dictionary[EditorForm.CurrentLanguage] = value;
            }
        }

        [XmlIgnore]
        public Element Parent { get; set; }

        [XmlIgnore]
        public virtual string Path
        {
            get
            {
                string path = DefaultName;

                for (Element parent = Parent; parent != null; parent = parent.Parent)
                    path = System.IO.Path.Combine(parent.DefaultName,
                        path);

                return path;
            }
        }

        public Element()
        {
            Dictionary = new SDictionary<string>();
        }

        public Element(SDictionary<string> dictionary)
        {
            Dictionary = dictionary;
        }

        public abstract Element CreateChildElement(SDictionary<string> dictionary);

        public virtual void AddChildElement(Element element)
        {
            element.Parent = this;
            ChildElements.Add(element);
        }

        public virtual Element AddChildElement(SDictionary<string> dictionary)
        {
            Element elem = CreateChildElement(dictionary);

            AddChildElement(elem);

            return elem;
        }

        public virtual void RemoveChildElement(Element element)
        {
            ChildElements.Remove(element);
        }

        /*
        public virtual void RemoveChildElement(string elementName)
        {
            RemoveChildElement(CreateChildElement(elementName));
        }
         * */

        public void Remove()
        {
            if (Parent != null)
                Parent.RemoveChildElement(this);
        }

        public int CompareTo(object obj)
        {
            if (GetType() != obj.GetType())
                throw new ArgumentException(GetType() + " türü ile " 
                    + obj.GetType() + " türü arasında mantıksız karşılaştırma!");

            return Path.CompareTo((obj as Element).Path);
        }

        public override bool Equals(object obj)
        {
            return GetType() == obj.GetType() && Path.Equals((obj as Element).Path);
        }

        public override int GetHashCode()
        {
            return Path.GetHashCode();
        }

        public SDictionary<string> Dictionary { get; set; }
    }
}
